//
//  BSloginAndRegisterViewController.m
//  BS
//
//  Created by 王浩宇 on 15/8/15.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "BSloginAndRegisterViewController.h"
#import "Header.h"
#import "PKRequestManager.h"
#import "BSCaptchaViewController.h"

#import <CommonCrypto/CommonCryptor.h>


@interface BSloginAndRegisterViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *loginPhoneText;
@property (weak, nonatomic) IBOutlet UITextField *loginPasswordText;
@property (weak, nonatomic) IBOutlet UIButton *backButton;
@property (weak, nonatomic) IBOutlet UIButton *registerButton;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (nonatomic, strong)UITextField *phoneTextField;
@property (nonatomic, strong)UITextField *passwordTextField;
@property (nonatomic, strong)UIButton *regisButton;

@end

@implementation BSloginAndRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creationScroll];
 
    // Do any additional setup after loading the view from its nib.
}
- (void)creationScroll
{
    _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * 2, 175);
    _scrollView.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0];
    
    _phoneTextField = [[UITextField alloc] initWithFrame:CGRectMake(36, 29, SCREEN_WIDTH - 70, 36)];
    _phoneTextField.placeholder = @"请输入手机号";
    _passwordTextField = [[UITextField alloc] initWithFrame:CGRectMake(36, 63, SCREEN_WIDTH - 70, 36)];
    _passwordTextField.placeholder = @"请输入密码";
    _regisButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_regisButton setTitle:@"注册" forState:UIControlStateNormal];
    _regisButton.frame = CGRectMake(36, 115, SCREEN_WIDTH - 70, 36);
    _regisButton.backgroundColor = [UIColor redColor];
    [_regisButton addTarget:self action:@selector(doRegister:) forControlEvents:UIControlEventTouchUpInside];
    _phoneTextField.borderStyle = UITextBorderStyleLine;
    _passwordTextField.borderStyle = UITextBorderStyleLine;
    _phoneTextField.delegate = self;
    _passwordTextField.delegate = self;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, 175)];
    [view addSubview:_phoneTextField];
    [view addSubview:_passwordTextField];
    [view addSubview:_regisButton];
    [_scrollView addSubview:view];

    
}
//返回
- (IBAction)backButton:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
//切换登陆注册
- (IBAction)registerLogin:(id)sender {
    
    if (_registerButton.selected == NO) {
        _registerButton.selected = YES;
        _scrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
    }else if(_registerButton.selected == YES){
        _registerButton.selected = NO;
        _scrollView.contentOffset = CGPointMake(0, 0);
    }
    
}

- (void)doRegister:(UIButton *)button
{
    NSDate *  senddate=[NSDate date];
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"YYYYMMddHHmmss"];
    NSString *  locationString=[dateformatter stringFromDate:senddate];
    NSLog(@"locationString:%@",locationString);
    NSString *url = [NSString stringWithFormat:@"https://api.budejie.com/api/api_open.php?a=getverify&c=user&device_id=9A2EA4AE-2DEB-4479-A977-8CB5B3EFF60F&phonenum=%@&sec=5e59d9ceddda20d017e23251d7a885ff&time=%@&verifytype=1",self.phoneTextField.text,locationString];
    NSLog(@"url:%@",url);
    [PKRequestManager manager:GET requestWith:url parDic:nil finish:^(NSData *data) {
        NSLog(@"data:%@",data);
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"dict%@",dict);
        if ([dict[@"code"] integerValue] == 1000 ) {
            BSCaptchaViewController *captchVC = [[BSCaptchaViewController alloc] init];
            [self.navigationController pushViewController:captchVC animated:YES];
        }else{
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:dict[@"msg"] delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alertView show];

        }
        

    } error:^(NSError *error) {
        
    }];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    return YES;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
//登陆
- (IBAction)loginButton:(id)sender {
    NSString *pass = [_loginPasswordText.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    _loginPasswordText.text = @"1dhF3NblXMGtxcdFFhz9LQ%3D";
    NSString *str = [NSString stringWithFormat:@"https://api.budejie.com/api/api_open.php?a=login&appname=baisishequ&asid=9A2EA4AE-2DEB-4479-A977-8CB5B3EFF60F&c=user&password=%@&phonenum=%@",pass,_loginPhoneText.text];
    NSLog(@"str:%@",str);
    [PKRequestManager manager:GET requestWith:str parDic:nil finish:^(NSData *data) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"dict:%@",dict[@"msg"]);
    } error:^(NSError *error) {
        
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
